﻿Public Class PlaylistEntryEditor
  Private _avple As Playlists.AVPLPlaylistEntry = Nothing
  Private _db As AvianPlayDB = Nothing
  Private _filenameUpdated As Boolean = True

  Public Sub New()
    InitializeComponent()

    Me.LabelFilePath.Text = Language.GetLanguageString("EditorFilePath")
    Me.LabelTitle.Text = Language.GetLanguageString("EditorStreamTitle")
    Me.LabelRefreshURL.Text = Language.GetLanguageString("EditorSourceRefreshURL")
    Me.LabelStreamURLs.Text = Language.GetLanguageString("EditorStreamURLs")
    Me.MenuItemOK.Text = Language.GetLanguageString("OK")
    Me.MenuItemMenu.Text = Language.GetLanguageString("Menu")
    Me.MenuItemCancel.Text = Language.GetLanguageString("Cancel")
    Me.MenuItemAdd.Text = Language.GetLanguageString("EditorAddStreamURL")
    Me.MenuItemRemove.Text = Language.GetLanguageString("EditorRemoveStreamURL")
    Me.MenuItemRefresh.Text = Language.GetLanguageString("EditorRefreshURLsFromSource")
    Me.MenuItemClear.Text = Language.GetLanguageString("EditorClearStreamURLList")
    Me.MenuItemUp.Text = Language.GetLanguageString("EditorUpStreamURL")
    Me.MenuItemDown.Text = Language.GetLanguageString("EditorDownStreamURL")

    Me.InputPanelMain_EnabledChanged(Me, EventArgs.Empty)
  End Sub

  Public WriteOnly Property Database() As AvianPlayDB
    Set(ByVal value As AvianPlayDB)
      Me._db = value
    End Set
  End Property

  ''' <summary>
  ''' Copies the entry that is passed in; does not use the same reference.  After retrieving value, the thumbnails will NOT be populated, so don't forget to do that, if you need it!  :-)
  ''' </summary>
  Public Property PlaylistEntry() As Playlists.AVPLPlaylistEntry
    Get
      Return Me._avple
    End Get
    Set(ByVal value As Playlists.AVPLPlaylistEntry)
      Me._avple = New Playlists.AVPLPlaylistEntry(value, False)

      Me._avple.StreamUsePlaylistUrlOnce = False

      If Me._avple.IsFile Then
        If mainMenu1.MenuItems.Contains(Me.MenuItemMenu) Then
          Me.MenuItemMenu.MenuItems.Remove(Me.MenuItemCancel)
          Me.mainMenu1.MenuItems.Remove(Me.MenuItemMenu)
          Me.mainMenu1.MenuItems.Add(Me.MenuItemCancel)
        End If

        Me.PanelFile.Enabled = True
        Me.PanelFile.Visible = True
        Me.PanelFile.BringToFront()
        Me.TextBoxFile.Text = Me._avple.FilePath
        Me.TextBoxFile.SelectionStart = Me.TextBoxFile.Text.Length  ' Scroll the input box all the way to the right
        Me.TextBoxFile.SelectionLength = 0
        Me.TextBoxFile.Focus()
        Me._filenameUpdated = False
      ElseIf Me._avple.IsStream Then
        If mainMenu1.MenuItems.Contains(Me.MenuItemCancel) Then
          Me.mainMenu1.MenuItems.Remove(Me.MenuItemCancel)
          Me.mainMenu1.MenuItems.Add(Me.MenuItemMenu)
          Me.MenuItemMenu.MenuItems.Add(Me.MenuItemCancel)
        End If

        Me.PanelStream.Enabled = True
        Me.PanelStream.Visible = True
        Me.PanelStream.BringToFront()
        Me.TextBoxTitle.Text = Me._avple.StreamTitle
        Me.TextBoxRefreshURL.Text = Me._avple.StreamPlaylistUrlString

        Me.ListBoxURLs.Items.Clear()
        For Each url As Uri In Me._avple.StreamURLs
          Me.ListBoxURLs.Items.Add(url.ToString)
        Next

        Me.TextBoxTitle.Focus()
      Else
        ErrorLog.WriteToErrorLog("PlaylistEntryEditor :: Invalid AVPLE type at load :: " & Me._avple.Type.ToString)
        Me.DialogResult = Windows.Forms.DialogResult.Abort
        Me.Close()
      End If
    End Set
  End Property




  Public Sub SetColors(ByVal fg As Color, ByVal bg As Color, ByVal tbbg As Color)
    Me.BackColor = bg
    Me.PanelTop.BackColor = bg
    Me.PanelPushUp.BackColor = bg
    Me.PanelFile.BackColor = bg
    Me.PanelStream.BackColor = bg
    Me.TextBoxFile.BackColor = tbbg
    Me.TextBoxFile.ForeColor = fg
    Me.TextBoxTitle.BackColor = tbbg
    Me.TextBoxTitle.ForeColor = fg
    Me.TextBoxRefreshURL.BackColor = tbbg
    Me.TextBoxRefreshURL.ForeColor = fg
    Me.ListBoxURLs.BackColor = tbbg
    Me.ListBoxURLs.ForeColor = fg
    Me.WP7ButtonBrowseFile.BackColor = bg
    Me.WP7ButtonBrowseFile.ForeColor = fg
    Me.LabelTitle.BackColor = bg
    Me.LabelTitle.ForeColor = fg
    Me.LabelRefreshURL.BackColor = bg
    Me.LabelRefreshURL.ForeColor = fg
    Me.LabelStreamURLs.BackColor = bg
    Me.LabelRefreshURL.ForeColor = fg
  End Sub



  Private Sub InputPanelMain_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanelMain.EnabledChanged
    If Me.InputPanelMain.Enabled Then
      Me.PanelPushUp.Height = Me.InputPanelMain.Bounds.Height
    Else
      Me.PanelPushUp.Height = 0
    End If
  End Sub

  Private Sub MenuItemOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemOK.Click
    If Me._avple.IsFile Then
      If Me.TextBoxFile.Text.Trim.Count <= 0 OrElse Not IO.File.Exists(Me.TextBoxFile.Text.Trim) Then
        MessageBox.Show(Language.GetLanguageString("FileNotFound") & " " & Me.TextBoxRefreshURL.Text, "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
        Exit Sub
      ElseIf Me._filenameUpdated AndAlso String.Compare(Me._avple.FilePath.Trim, Me.TextBoxFile.Text.Trim, True) <> 0 Then
        Dim avple As Playlists.AVPLPlaylistEntry = FileSelectionDialogs.GetAudioFile(Me._db, Me.TextBoxFile.Text)
        If avple Is Nothing Then
          Exit Sub ' File is invalid
        Else
          Me._avple = avple
        End If
      End If
    ElseIf Me._avple.IsStream Then
      If Me.TextBoxTitle.Text.Trim.Length <= 0 Then
        MessageBox.Show(Language.GetLanguageString("EditorNoStreamTitle"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
        Exit Sub
      ElseIf Me.TextBoxRefreshURL.Text.Trim.Length <= 0 AndAlso Me.ListBoxURLs.Items.Count <= 0 Then
        MessageBox.Show(Language.GetLanguageString("EditorNoSouceURLs"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
        Exit Sub
      End If

      Me._avple.StreamTitle = Me.TextBoxTitle.Text.Trim

      Try
        Me._avple.StreamPlaylistUrlString = Me.TextBoxRefreshURL.Text
      Catch ex As Exception
        MessageBox.Show(Language.GetLanguageString("SourceRefreshInvalid") & " " & Me.TextBoxRefreshURL.Text, "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
        Exit Sub
      End Try

      If Me.ListBoxURLs.Items.Count > 0 Then
        Me._avple.StreamURLs.Clear()
        For Each urlstr As String In Me.ListBoxURLs.Items
          Try
            Dim url As New Uri(urlstr)
            Me._avple.StreamURLs.Add(url)
          Catch ex As Exception
            MessageBox.Show(Language.GetLanguageString("StreamRefreshInvalid") & " " & urlstr, "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
            Exit Sub
          End Try
        Next
      Else
        MenuItemRefresh_Click(Me, EventArgs.Empty)
      End If
    Else
      ErrorLog.WriteToErrorLog("PlaylistEntryEditor :: Invalid AVPLE type at OK click :: " & Me._avple.Type.ToString)
      Me.DialogResult = Windows.Forms.DialogResult.Abort
      Me.SendToBack()
      Me.Close()
      Exit Sub
    End If

    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.SendToBack()
    Me.Close()
  End Sub

  Private Sub MenuItemAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemAdd.Click
    Dim badUrl As Boolean = False
    Dim lastStr As String = String.Empty
    Do
      badUrl = False

      Dim gs As New GetStringDialog
      gs.BackColor = Me.BackColor
      gs.BackColorTextBox = Me.TextBoxFile.BackColor
      gs.ForeColor = Me.ForeColor
      gs.Label = Language.GetLanguageString("EditorAddStreamURL")
      gs.String = lastStr
      If gs.ShowDialog = Windows.Forms.DialogResult.OK Then
        Try
          lastStr = gs.String
          Me.ListBoxURLs.Items.Add(New Uri(gs.String).ToString)
        Catch
          MessageBox.Show(Language.GetLanguageString("StreamURLInvalid"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
          badUrl = True
        End Try
      End If
    Loop While badUrl
  End Sub

  Private Sub MenuItemRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemRemove.Click
    If Me.ListBoxURLs.SelectedIndex >= 0 Then
      Me.ListBoxURLs.Items.RemoveAt(Me.ListBoxURLs.SelectedIndex)
    End If
  End Sub

  Private Sub MenuItemUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemUp.Click
    Dim idx As Integer = Me.ListBoxURLs.SelectedIndex
    If idx >= 1 Then
      Dim item As String = Me.ListBoxURLs.Items(idx).ToString
      Me.ListBoxURLs.Items.RemoveAt(idx)
      Me.ListBoxURLs.Items.Insert(idx - 1, item)
      Me.ListBoxURLs.SelectedIndex = idx - 1
    End If
  End Sub

  Private Sub MenuItemDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemDown.Click
    Dim idx As Integer = Me.ListBoxURLs.SelectedIndex
    If idx >= 0 AndAlso idx < Me.ListBoxURLs.Items.Count - 1 Then
      Dim item As String = Me.ListBoxURLs.Items(idx).ToString
      Me.ListBoxURLs.Items.RemoveAt(idx)
      Me.ListBoxURLs.Items.Insert(idx + 1, item)
      Me.ListBoxURLs.SelectedIndex = idx + 1
    End If
  End Sub

  Private Sub MenuItemRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemRefresh.Click
    Try
      Windows.Forms.Cursor.Current = Cursors.WaitCursor
      Me._avple.RefreshStreamURLsFromPlaylistURL()
    Catch ex As Exception
      MessageBox.Show(Language.GetLanguageString("UnableToRefreshStreamURLs"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
    Finally
      Windows.Forms.Cursor.Current = Cursors.Default
    End Try
  End Sub

  Private Sub MenuItemClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemClear.Click
    Me.ListBoxURLs.Items.Clear()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.SendToBack()
    Me.Close()
  End Sub

  Private Sub WP7ButtonBrowseFile_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonBrowseFile.MouseUp
    Dim avple As Playlists.AVPLPlaylistEntry = FileSelectionDialogs.GetAudioFile(Me._db)
    If avple IsNot Nothing Then
      Me._avple = avple
      Me.TextBoxFile.Text = Me._avple.FilePath
      Me.TextBoxFile.SelectionStart = Me.TextBoxFile.Text.Length  ' Scroll the input box all the way to the right
      Me.TextBoxFile.SelectionLength = 0
      Me.TextBoxFile.Focus()
      Me._filenameUpdated = False
    End If
  End Sub

  Private Sub TextBoxFile_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxFile.TextChanged
    Me._filenameUpdated = True
  End Sub

  Private Sub MenuItemMenu_PopUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemMenu.Popup
    If Me._avple.IsStream Then
      Me.MenuItemClear.Enabled = Me.ListBoxURLs.Items.Count > 0
      Me.MenuItemRefresh.Enabled = Me.TextBoxRefreshURL.Text.Trim.Length > 0
      Me.MenuItemRemove.Enabled = Me.ListBoxURLs.SelectedIndex >= 0
      Me.MenuItemUp.Enabled = Me.ListBoxURLs.SelectedIndex >= 1
      Me.MenuItemDown.Enabled = Me.ListBoxURLs.SelectedIndex >= 0 AndAlso Me.ListBoxURLs.SelectedIndex < Me.ListBoxURLs.Items.Count - 1
    End If
  End Sub

End Class